# coding: utf-8
from BigPrize.GameRule.BP import BigPrize
from BigPrize.models import Draw, UserVote

__author__ = 'zhouqi'

class Game(object):
    """
    对玩法的控制
    包括创建新奖期、对已有奖期开奖
    奖期状态只有2种0在售1已开奖。创建奖期的逻辑里会保证同一时间同一玩法只有一个奖期在售。此外不在设置奖期时间仅以状态判断
    """

    def CreateDraw(self, no, gameId):
        """
        将判断no,gameId是否重复
        """
        if len(Draw.objects.filter(no=no, gameId=gameId)) == 1:
            raise Exception
        if len(Draw.objects.filter(gameId=gameId, status=0)) > 0:
            raise Exception
        d = Draw()
        d.no = no
        d.gameId = gameId
        d.status = 0
        d.save()
        return

    def PayForDraw(self, id, result, przLvls):
        """

        """
        list = Draw.objects.filter(id=id)
        if len(list) <> 1:
            raise Exception
        draw = list[0]
        if draw.status == 1:
            raise Exception
        tickets = UserVote.objects.filter(drawId=id)
        for win in BigPrize.ComputeDrawPrz(result, przLvls, tickets):
            win.save()
        draw.status = 1
        draw.result = result
        draw.przLvl = przLvls
        draw.save()
        return
